package cn.ghf.user.service.impl;

import cn.ghf.basic.exception.MyException;
import cn.ghf.basic.service.impl.BaseServiceImpl;
import cn.ghf.basic.util.MD5Utils;
import cn.ghf.basic.util.StrUtils;
import cn.ghf.user.domain.LoginInfo;
import cn.ghf.user.domain.User;
import cn.ghf.user.dto.UserDto;
import cn.ghf.user.mapper.LoginInfoMapper;
import cn.ghf.user.mapper.UserMapper;
import cn.ghf.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 手机号注册
     * @param userDto
     */
    @Override
    @Transactional
    public void register(UserDto userDto) {
        //1.非空校验
        if(StringUtils.isEmpty(userDto.getPhone())||StringUtils.isEmpty(userDto.getSmsCode())||
        StringUtils.isEmpty(userDto.getPassword())||StringUtils.isEmpty(userDto.getPasswordRepeat())){
            throw new MyException("请输入完整相关信息");
        }
        //2.手机号是否被注册
        User user = userMapper.loadByPhone(userDto.getPhone());
        if(user!=null){
            throw new MyException("当前手机号已注册，请直接登录");
        }

        //3.确认密码校验
        if(!userDto.getPassword().equals(userDto.getPasswordRepeat())){
            throw new MyException("两次密码不一样，请重新输入");
        }

        //4.验证码是否有效
        Object o = redisTemplate.opsForValue().get("register:" + userDto.getPhone());
        if(o==null){
            throw new MyException("验证码已失效，请重新获取");
        }
        //5.验证码是否正确
        String code = o.toString().split(":")[0];
        if(!userDto.getSmsCode().equalsIgnoreCase(code)){
            throw new MyException("验证码错误，请重新输入");
        }
        //6.添加数据
        //先准备一个user表
        User userTmp = userDto2user(userDto);
        //在准备一个loginInfo表
        LoginInfo loginInfo = user2LoginInfo(userTmp);

        //添加数据
        loginInfo.setType(true);
        loginInfoMapper.add(loginInfo);

        userTmp.setLogininfo_id(loginInfo.getId());
        userMapper.add(userTmp);

    }

    private LoginInfo user2LoginInfo(User user) {
        LoginInfo loginInfo = new LoginInfo();
        BeanUtils.copyProperties(user, loginInfo);
        return loginInfo;
    }

    private User userDto2user(UserDto userDto) {

        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());

        String salt = StrUtils.getComplexRandomString(32);
        user.setSalt(salt);
        String password = MD5Utils.encrypByMd5(userDto.getPassword() + salt);
        user.setPassword(password);

        return user;
    }
}
